/*
 * Code for blog.techhead.biz
 * Distributed under BSD-style license
 */

package biz.techhead.qbpos;

/**
 *
 * @author jonathan
 */
public class SimpleResponseProcessor implements ResponseProcessor {

    public synchronized void processResponse(String qbposXML) {
        responseXML = qbposXML;
        responseReceived = true;
        notify();
    }
    
    public synchronized String waitForResponseXML(long timeout) {
        if (!responseReceived) {
            try {
                wait(timeout);
            } catch (InterruptedException e) {
                throw new QBPOSException("Interrupted while waiting for QBPOS response.");
            }
            if (!responseReceived) {
                throw new QBPOSException("Timed out while waiting for QBPOS response.");
            }
        }
        if (responseXML==null || responseXML.indexOf("<QBPOSXML")==-1) {
            throw new QBPOSException("QBPOS response unexpected - " + responseXML);
        }
        return responseXML;
    }
    
    private String responseXML;
    private boolean responseReceived;
}
